package Engine.UIControl
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	public class ScrollText
	{
		public static const SCROLL_TEXT_UP:int = 1;
		public static const SCROLL_TEXT_DOWN:int = 2;
		
		private var _scrollType:int = 1;
		
		private var _textField:TextField;
		
		private var _dragbtn:DragGUI;	//滚动条
		private var _upBtn:Button;		//上按钮
		private var _downBtn:Button;	//下按钮
		
		private var _mWheelCallbackID:int = 0;
		
		public function ScrollText(textFd:TextField,upbtn:Sprite,downbtn:Sprite,dragbtn:Sprite)
		{
			
			this._textField = textFd;
			this._textField.addEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheel);
			
			this._dragbtn = new DragGUI(dragbtn);
			this._dragbtn.setMinMaxY(upbtn.y+upbtn.height,downbtn.y);
			this._dragbtn.setUpdateYFun(_updateY);
			
			this._upBtn = new Button(upbtn);
			this._upBtn.setOnClickFun(onUp);
			
			this._downBtn = new Button(downbtn);
			this._downBtn.setOnClickFun(onDown);
			
		}
		
		private function onUp():void
		{
			if(this._scrollType == SCROLL_TEXT_UP)
			{
				this._down();
			}
			else
			{
				this._up();
			}
		}
		
		private function onDown():void
		{
			if(this._scrollType == SCROLL_TEXT_UP)
			{
				this._up();
			}
			else
			{
				this._down();
			}
		}
		
		private function _up():void
		{
			if(this.scrollV >= this.maxScrollV)
				return;
			this.scrollV = this.scrollV + 1;
			setMoveBarPos();
		}
		
		private function _down():void
		{
			if(this.scrollV <= 0)
				return;
			this.scrollV = this.scrollV - 1;
			setMoveBarPos();
		}
		
		private function onMouseWheel(event:MouseEvent):void
		{
			if(this._mWheelCallbackID == 0 )
				this._mWheelCallbackID = setTimeout(setMoveBarPos,50);
		}
		
		private function setMoveBarPos():void
		{
			this._mWheelCallbackID = 0;
			if(this.maxScrollV)
			{
				this._dragbtn.y = this._dragbtn.dragHeight* this.scrollV / this.maxScrollV;
			}
			else
			{
				if(this.scrollV || this._scrollType == SCROLL_TEXT_DOWN)
					this._dragbtn.y = 0;
				else
					this._dragbtn.y = this._dragbtn.dragHeight;
			}
		}
		
		private function _updateY(offsetY:Number):void
		{
			var scrollV:int = int(offsetY*this.maxScrollV/this._dragbtn.dragHeight);
			this.scrollV = scrollV;
			if(this._textField.scrollV == this.maxScrollV)
			{
				this.scrollV = this.maxScrollV;
			}
		}
		
		private function get scrollV():int
		{
			return this._textField.scrollV - 1;
		}
		
		private function set scrollV(v:int):void
		{
			this._textField.scrollV = v + 1;
		}
		
		private function get maxScrollV():int
		{
			return this._textField.maxScrollV - 1;
		}
		
		public function resetBtn():void
		{
			if(this._textField.maxScrollV <= 1)
				this._dragbtn.setDrag(false,false);
			else
				this._dragbtn.setDrag(false,true);
		}
		
		public function setScrollTop():void
		{
			this.scrollV = this.maxScrollV;
			this.setMoveBarPos();
		}
		
		public function setScroButtom():void
		{
			this.scrollV = 0;
			this.setMoveBarPos();
		}
		
		public function setTextType(_type:int):void
		{
			this._scrollType = _type;
		}
	}
}